package com.topsun;

import com.topsun.protocol.framework.util.Explain;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import com.topsun.protocol.JT808Beans;
import com.topsun.protocol.basics.JTMessage;
import com.topsun.protocol.codec.JTMessageAdapter;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * 解码分析
 */
public class Elucidator extends JT808Beans {

    public static final JTMessageAdapter coder = new JTMessageAdapter("com.topsun.protocol");

    public static void main(String[] args) {
//        String hex = "020000d40123456789017fff000004000000080006eeb6ad02633df7013800030063200707192359642f000000400101020a0a02010a1e00640001b2070003640e200707192359000100000061646173200827111111010101652f000000410202020a0000000a1e00c8000516150006c81c20070719235900020000000064736d200827111111020202662900000042031e012c00087a23000a2c2a200707192359000300000074706d732008271111110303030067290000004304041e0190000bde31000d90382007071923590004000000006273642008271111110404049d";
//        String hex = "7E02000070018992000223001000000000000C000301E4C70F072B79F70013024400A724011119183901040001D7E00202000003020262140480000000150400000000160400000000170200002504000000002B040000000030011F31011CEB0D000300D400000600F880000000EF0D00000000000049249200001103BF7E";
//        String hex = "7E020000A1018992000223000D00000000000C000301E4CA61072B791E0011024E00A724011119183301040001D7DF020200000302024E140480000000150400000000160400000000170200002504000000002B040000000030011F31011C642F0000000600020200000100003B000001E4CAF7072B78FD240111191831044132303030323233240111191831000500EB0D000300D400000600F880000000EF0D000000000000492492000011039F7E";
//        String hex = "020000cd018992000223002d00000000000c00c301594bc406cc9558005a0000000024011308563401040000000f020200000302000030011431010e2b040000000766950000002c0000005a01594bc406cc95582401130856340401323030303232332401130856342c00000c0000000380001400500100000380001400500200000380001400500300000380001400500400000380001400500500000380001400500600000380001400500700000380001400500800000380001400500900000380001400500a00000380001400500b0000038000140050b97e";
        String hex = "7E0200007F040516165543028800000000004C000301E785730714FBD10019016000A9240126132840010400078302030201602504000000002B040000000030011431010B14040000000015040000000016040000000017020000652F00006569000701000000000021001A01E786D20714FB832401261328400401313130373137312401261328401204005B7E";
//        JTMessage msg = H2019(T0200JSATL12());

        List<String> hexList = new ArrayList<>();
        hexList.add("7E9101001501599100113600010D3131372E37382E34372E3234371E190000060001437E");
        hexList.add("7e91010013015623035197009c0b34392e342e32322e3134341e180000020000877e");

        for (String aa : hexList) {
        JTMessage msg = decode(aa);

        }
//        hex = "7E020001100139920046260F8800000000004C000301E85AD407167FDA002C005F005324020410580701040004B68E0302005A1404000000002A02C0001504000000001604000000001702000030011F310117EAAC6268632F72334F34584D51454456474873586779324368672F30516549506F3534566C76337A5339706C34357A4F6150757A746A67715079436C41324F6153796174784F66784536396B4930724A614958357078383874775847414E7649746349747052755769717A57765745566756776B4B795639635345674B69673775364264563354694A33304766477A7A35457151666F52794648756A4176434F73366C304A507A322B324A41513DEB050000000000EF0D00000000000000000000000000250400000000697E";
//        decode(hex);
//        hex = encode(msg);
    }

    private static String encode(JTMessage message) {
        System.out.println("====================================================================================\n");
        Explain explain = new Explain();
        ByteBuf buf = null;
        try {
            buf = coder.encode(message, explain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String hex = ByteBufUtil.hexDump(buf);
        System.out.println(message);
        System.out.println(hex);
        explain.println();
        return hex;
    }

    private static JTMessage decode(String hex) {
        System.out.println("====================================================================================\n");
        Explain explain = new Explain();
        JTMessage message = null;
        try {
            message = coder.decode(Unpooled.wrappedBuffer(ByteBufUtil.decodeHexDump(hex)), explain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(message);
        System.out.println(hex);
        explain.println();
        return message;
    }
}